//
//  UnuseCouponViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/5.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "UnuseCouponViewController.h"
#import "DiscountCouponCell.h"

@interface UnuseCouponViewController ()<UITableViewDelegate,UITableViewDataSource>
/**
 myTableView
 */
@property (nonatomic , strong) UITableView *myTableView;
/**
 dataArr
 */
@property (nonatomic , strong) NSMutableArray *dataArr;
/**
 page
 */
@property (nonatomic , assign) int page;
/**
 limit
 */
@property (nonatomic , assign) int limit;

@end

@implementation UnuseCouponViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.myTableView];
}

- (void) initLayout {
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void) initAction {
    
}

- (void)initData {

    self.limit = 5;
    [self dataRequest];
    
    @weakify(self);
    self.myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self);
        [self dataRequest];
    }];    
    self.myTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++ ;
        @strongify(self);
        [self moreDataRequest];
    }];
    self.myTableView.mj_footer.hidden = YES;

}

#pragma mark - delegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiscountCoupons *model = self.dataArr[indexPath.row];
    if (model.isSelect) {
        return 165;
    }
    return 115;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DiscountCouponCell *cell = [DiscountCouponCell cellWithTableView:tableView];
    if (self.dataArr.count > 0 && self.dataArr.count > indexPath.row) {
        DiscountCoupons *model = self.dataArr[indexPath.row];
        [cell configCellWithModel:model discountCouponsType:DiscountCouponsTypeUnUse];
        [cell setUseDetailBlock:^(BOOL isSelect) {
            model.isSelect = isSelect;
            [tableView beginUpdates];
            [tableView endUpdates];
        }];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark - target action

#pragma mark - private method
- (void)dataRequest {
    [self.dataArr removeAllObjects];
    self.page = 1;
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    DiscountCouponsParam *param = [[DiscountCouponsParam alloc] init];
    param.page = self.page;;
    param.limit = self.limit;
    param.memberId = memberId;
    param.type = @1;
    @weakify(self);
    [[[PigMarketApi sharedInstance] userDiscountCouponsWithParam:param] subscribeNext:^(DiscountCouponsList * _Nullable discountCouponsList) {
        @strongify(self);
        
        [self.dataArr addObjectsFromArray:discountCouponsList.list];
        
        if (self.discountCouponsCountBlock) {
            self.discountCouponsCountBlock([discountCouponsList.totalCount intValue]);
        }
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        if (self.page == [discountCouponsList.totalPage intValue] || [discountCouponsList.totalPage intValue] == 0) {
            [self.myTableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            self.myTableView.mj_footer.hidden = NO;
        }
       
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}
- (void)moreDataRequest {
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    DiscountCouponsParam *param = [[DiscountCouponsParam alloc] init];
    param.page = self.page;
    param.limit = self.limit;
    param.memberId = memberId;
    param.type = @1;
    @weakify(self);
    [[[PigMarketApi sharedInstance] userDiscountCouponsWithParam:param] subscribeNext:^(DiscountCouponsList * _Nullable discountCouponsList) {
        @strongify(self);
        
        [self.dataArr addObjectsFromArray:discountCouponsList.list];
        if (self.page == [discountCouponsList.totalPage intValue]) {
            [self.myTableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];

    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
    }];
}
#pragma mark - getter and setter
- (UITableView *)myTableView {
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableView.backgroundColor = [UIColor whiteColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _myTableView;
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}
@end

